package net.lab1024.sa.admin.module.business.bjd.service;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.admin.module.business.bjd.domain.entity.OrderEntity;
import net.lab1024.sa.admin.module.business.bjd.domain.form.OrderImportForm;
import net.lab1024.sa.admin.module.business.bjd.domain.form.OrderQueryForm;
import net.lab1024.sa.admin.module.business.bjd.domain.form.OrderUpdateForm;
import net.lab1024.sa.admin.module.business.bjd.domain.vo.DollVO;
import net.lab1024.sa.admin.module.business.bjd.domain.vo.OrderVo;
import net.lab1024.sa.admin.module.business.bjd.dto.OrderImportDto;
import net.lab1024.sa.admin.module.business.bjd.mapper.OrderMapper;
import net.lab1024.sa.admin.module.business.goods.domain.form.GoodsImportForm;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.exception.BusinessException;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 类的描述
 *
 * @author guolong.zhang
 * @date 2025/02/09 09:36
 **/
@Service
@Slf4j
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;
    public PageResult<OrderVo> queryPage(OrderQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<OrderVo> list = orderMapper.pageList(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    @Transactional(rollbackFor = Exception.class)
    public void importOrder(MultipartFile file) {
        List<OrderImportDto> dataList;
        try {
            dataList = EasyExcel.read(file.getInputStream()).head(OrderImportDto.class)
                    .sheet()
                    .doReadSync();
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw new BusinessException("数据格式存在问题，无法读取");
        }
        if (CollectionUtils.isEmpty(dataList)) {
            throw new BusinessException("数据为空");
        }
        if(dataList.size() > 200){
            throw new BusinessException("数据不能超过200条");
        }

        log.info("导入数据解析完成{}条",dataList.size());
        List<OrderEntity> orderEntityList = new ArrayList<>();
        for (OrderImportDto dto : dataList) {
            log.info("{}",dto);
            if(null == dto.getOrderNo()){
                continue;
            }
            OrderEntity orderEntity = Db.lambdaQuery(OrderEntity.class)
                    .eq(OrderEntity::getOrderNo, dto.getOrderNo())
                    .last(" limit 1 ")
                    .one();
            OrderEntity order = new OrderEntity();
            if(orderEntity != null){
                //有历史数据，跳过
                continue;
            }
            order.setDollNo(dto.getDollNo());
            order.setOrderNo(dto.getOrderNo());
            order.setLogisticsNo(dto.getLogisticsNo());
            order.setDollName(dto.getDollName());
            order.setDollPhone(dto.getDollPhone());
            orderEntityList.add(order);
        }
        if(CollUtil.isNotEmpty(orderEntityList)){
            Db.saveBatch(orderEntityList);
        }
    }

    public void updateOrder(OrderUpdateForm req) {
        OrderEntity order = Db.getById(req.getId(), OrderEntity.class);
        if(null == order){
            throw new BusinessException("订单不存在");
        }
        order.setOrderNo(req.getOrderNo());
        order.setLogisticsNo(req.getLogisticsNo());
        order.setDollName(req.getDollName());
        order.setDollPhone(req.getDollPhone());
        order.setDollNo(req.getDollNo());
        Db.updateById(order);
    }
}
